package com.sihai.ehr.service.impl;

import java.text.MessageFormat;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.QueryCreator;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.TrainAssessWeight;
import com.sihai.ehr.service.TrainAssessWeightService;

/**
 * 培训训练权重服务实体类
 * 
 * @author henry
 * 
 */
@Service("trainAssessWeightService")
public class TrainAssessWeightServiceImpl implements TrainAssessWeightService {

	@Resource
	DataAccessor dataAccessor;

	@Override
	public TrainAssessWeight getTrainAssessWeight(Long weightId) {
		if (weightId == null || weightId < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.get(TrainAssessWeight.class, weightId);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取培训权重明细数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：TrainAssessWeightServiceImpl.getTrainAssessWeight;参数id="
							+ weightId + " 】");
		}
	}

	@Override
	public TrainAssessWeight getTrainAssessWeightByTitle(final String title,
			final int assessType) {
		if (title == null || title.isEmpty()) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findUnique(TrainAssessWeight.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from TrainAssessWeight t where 1 <> 2");
							dynamicQuery.append(" and t.title='" + title + "'");
							dynamicQuery.append(" and t.assessType="
									+ assessType);
							return dynamicQuery.build(session);
						}
					});
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取培训权重明细数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：TrainAssessWeightServiceImpl.getTrainAssessWeightByTitle;参数title="
							+ title + ",assessType=" + assessType + "】");
		}
	}

	@Override
	public LimitedList<TrainAssessWeight> getTrainAssessWeights(
			final Long organizationId, final Long dutyId, final int assessType,
			Limit limit, Sorter sorter) {
		if (organizationId == null || organizationId < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findByQuery(TrainAssessWeight.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from TrainAssessWeight t where 1 <> 2");
							// dynamicQuery.append(" and t.employeeinfoWork.organization.treePath like '%,"
							// + organizationId + ",%'");
							// if (dutyId > 0) {
							// dynamicQuery.appendIfNotNull(" and t.employeeinfoWork.duty.id =:dutyId",
							// "dutyId", dutyId);
							// }
							dynamicQuery.append(" and t.assessType="
									+ assessType);
							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取培训权重时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：TrainAssessWeightServiceImpl.getTrainAssessWeights;参数organizationId="
							+ organizationId + ",dutyId=" + dutyId + "】");
		}
	}

	@Override
	public void save(TrainAssessWeight assessWeight) {
		if (assessWeight == null) {
			throw new RuntimeException("【assessWeight对象参数异常】");
		}
		try {
			dataAccessor.saveOrUpdate(assessWeight);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：保培训培训权重对象到数据库发生异常】【异常说明："
					+ e.getMessage()
					+ "】【发生地点：TrainAssessWeightServiceImpl.save】");
		}
	}

	@Override
	public void deleteTrainAssessWeight(final String ids) {
		if (ids == null || ids.isEmpty()) {
			throw new RuntimeException("【参数异常：ids为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat
							.format("delete from TrainAssessWeight t where t.id in ({0})",
									ids));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：移出培训培训权重信息数据库发生异常】【异常说明："
							+ e.getMessage()
							+ "】【发生地点：TrainAssessWeightServiceImpl.deleteTrainAssessWeight】");
		}
	}

}
